package com.qiongqi.datadir.utils.json;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

import java.text.SimpleDateFormat;

import static com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES;
import static com.fasterxml.jackson.core.json.JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER;
import static com.fasterxml.jackson.databind.DeserializationFeature.*;
import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS;
import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_SELF_REFERENCES;

public class JsonSerializer {
    public static final ObjectMapper OBJECT_MAPPER;
    static {
        OBJECT_MAPPER = new ObjectMapper();
        OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        OBJECT_MAPPER.enable(ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER.mappedFeature());
        OBJECT_MAPPER.disable(FAIL_ON_UNKNOWN_PROPERTIES);
        OBJECT_MAPPER.disable(FAIL_ON_INVALID_SUBTYPE);
        OBJECT_MAPPER.configure(FAIL_ON_EMPTY_BEANS, false);
        OBJECT_MAPPER.configure(FAIL_ON_NULL_FOR_PRIMITIVES, false);
        OBJECT_MAPPER.configure(ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
        OBJECT_MAPPER.configure(ALLOW_UNQUOTED_FIELD_NAMES, true);
        //OBJECT_MAPPER.configure(FAIL_ON_SELF_REFERENCES, false);
        OBJECT_MAPPER.setFilterProvider(new SimpleFilterProvider().setFailOnUnknownId(false));
        OBJECT_MAPPER.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        OBJECT_MAPPER.enable(ACCEPT_SINGLE_VALUE_AS_ARRAY);
        //OBJECT_MAPPER.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        //ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
    }
}
